package com.music.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.music.server.pojo.Song;
import com.music.server.service.SongService;
import com.music.server.utils.ResultVo;
import com.music.server.vo.SongVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 歌曲 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-08
 */
@RestController
@RequestMapping("song")
public class SongController {

    @Autowired
    private SongService songService;

    /**
     * 添加歌曲信息
     * @return
     */
    @PostMapping("save")
    public ResultVo save(@RequestBody Song song){
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        boolean save = songService.save(song);
        if (save) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 修改歌曲信息
     * @return
     */
    @PutMapping("update")
    public ResultVo update(@RequestBody Song song){
        song.setUpdateTime(new Date());
        boolean update = songService.updateById(song);
        if (update) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 查询所有的歌曲信息
     * @return
     */
    @GetMapping("list")
    public ResultVo list(){
        List<Song> list = songService.list();
        return ResultVo.ok().data("list",list);
    }

    /**
     * 分页条件查询所有的歌曲信息
     * @return
     */
    @PostMapping("page/{pageNum}/{size}")
    public ResultVo page(@PathVariable Integer pageNum,@PathVariable Integer size,@RequestBody SongVo songVo){

        return ResultVo.ok().data("page",songService.page(pageNum,size,songVo));
    }


    /**
     * 删除歌曲信息及对应的所有信息
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultVo delete(@PathVariable Integer id){
        boolean delete = songService.deleteById(id);
        if (delete) return ResultVo.ok();
        return ResultVo.error();
    }

    /**
     * 根据歌曲名称查询歌曲信息
     * @param name
     * @return
     */
    @GetMapping("findByName/{name}")
    public ResultVo findByName(@PathVariable String name){
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        songQueryWrapper.eq("name",name);
        return ResultVo.ok().data("song",songService.getOne(songQueryWrapper));
    }


}

